package findOcurrences;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public String[] findOcurrences(String text, String first, String second) {
        // 得到每个单词
        // 找到first 和 second
        List<String> list = new ArrayList<>();
        int i = 0,j = 0;
        while (j<text.length()) {
            if(j == text.length()-1) {
                list.add(text.substring(i,j+1));
                break;
            } else if (text.charAt(j) !=' '){
                j++;
            } else {
                list.add(text.substring(i,j));
                i = j+1;
                j++;
            }
        }
        List<String> ans = new ArrayList<>();
        for (int k = 0; k + 2 < list.size(); k++) {
            if(list.get(k).equals(first)) {
                if(list.get(k+1).equals(second)) {
                    ans.add(list.get(k+2));
                }
            }
        }
        return ans.toArray(new String[ans.size()]);
    }
}
